package com.oocl.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oocl.memory.SystemMemory;
import com.oocl.pojo.Constants;
import com.oocl.pojo.Merchant;

@WebFilter("/sec/dishes/*")
public class DishManageFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// place your code here
		SystemMemory systemMemory= SystemMemory.getInstance();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		HttpSession sess = req.getSession();
		Merchant m = (Merchant) sess.getAttribute("merchant");
		if(systemMemory.isBan(m.getmId())){
			request.getRequestDispatcher("/WEB-INF/views/banPage.jsp").forward(request, response);
			return;
		}
		
		if (m == null || m.getmStatus() != Constants.APPLY_PASSED) {
			resp.sendRedirect("../home");
			return;
		} else {
			// pass the request along the filter chain
			chain.doFilter(request, response);
		}
		
//		System.out.println("dish filter activated...");
//		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
